package cn.hdu.charge.servlet;

import cn.hdu.charge.domain.User;
import cn.hdu.charge.service.MyException;
import cn.hdu.charge.service.UserService;
import cn.hdu.utils.commons.CommonUtils;
import cn.hdu.utils.commons.Push;
import cn.hdu.utils.servlet.BaseServlet;
import com.google.gson.Gson;
import com.tencent.xinge.XingeApp;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户模块WEB层
 */
@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserService();
    private Map<String, String> json = new HashMap<>();
    private Gson gson = new Gson();

    public void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        User formUser = CommonUtils.toBean(req.getParameterMap(), User.class);

        json = validateRegist(formUser);
        if(json.size() > 0) {
            json.put("status", "-1");
        } else {
            userService.regist(formUser);
            json.put("status", "1");
            json.put("descr", "注册成功");
        }
        outResp(resp, gson.toJson(json));
    }

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        User formUser = CommonUtils.toBean(req.getParameterMap(), User.class);
        User user = userService.login(formUser);
        if(user == null) {
            json.put("status", "-1");
            json.put("descr", "用户名或密码错误");
            outResp(resp, gson.toJson(json));
        } else {
            Push.push(user.getMobile(), "登录成功", user.getUserId());
            outResp(resp, gson.toJson(user));
            System.out.println(gson.toJson(user));
        }
    }

    public void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, MyException {
        User formUser = CommonUtils.toBean(req.getParameterMap(), User.class);

        if(userService.updatePassword(formUser.getUserId(),
                formUser.getPassword(), formUser.getNewPassword())) {
            json.put("status", "1");
            json.put("descr", "重置密码成功");
        } else {
            json.put("status", "-1");
            json.put("descr", "重置密码失败");
        }
        outResp(resp, gson.toJson(json));

    }

    public void chargeBlance(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User formUser = CommonUtils.toBean(req.getParameterMap(), User.class);
        if(userService.chargeBlance(formUser.getUserId(), formUser.getBalance())) {
            json.put("status", "1");
            json.put("descr", "充值成功");
        } else {
            json.put("status", "-1");
            json.put("descr", "充值失败");
        }
        outResp(resp, gson.toJson(json));
    }

    private Map<String, String> validateRegist(User formUser) {

        Map<String, String> errors = new HashMap<>();

        /**
         * 校验登录密码
         */
        String password = formUser.getPassword();
        if(password == null || password.trim().isEmpty()) {
            errors.put("descr", "密码不能为空");
        } else if(password.length() < 3 || password.length() > 20) {
            errors.put("descr", "密码长度必须在3-20之间");
        }

        /**
         * mobile校验
         */
        String mobile = formUser.getMobile();
        if(mobile == null || mobile.trim().isEmpty()) {
            errors.put("descr", "手机号不能为空");
        } else if(mobile.length() != 11) {
            errors.put("descr", "手机号必须是11位");
        } else if(!userService.ajaxValidateMobile(mobile)) {
            errors.put("descr", "此手机号已被注册");
        }

        return errors;
    }

}
